/**
 * 
 */
package br.com.ufrn.aipetro.ga.controller.device;

import java.util.List;

import br.com.ufrn.aipetro.ga.controller.interfaces.AbstractController;
import br.com.ufrn.aipetro.ga.domain.entity.plant.Plant;
import br.com.ufrn.aipetro.ga.domain.entity.unit.Unit;
import br.com.ufrn.aipetro.ga.persist.interfaces.DaoFactory;

/**
 * @author Lucas
 *
 */
public class PlantController extends AbstractController<Plant> {
	
	DaoFactory daoFactory;
	
	public PlantController(DaoFactory factory) {
		this.daoFactory = factory;
		setModel(new Plant());
	}
	
	public void changeUnit(Unit unit) {
		getModel().setUnit(unit);
	}
	
	public void changeName(String name) {
		getModel().setName(name);
	}
	
	public void persistDelete(Plant plant) {
		daoFactory.beginTransaction();
		daoFactory.createPlantDao().delete(plant);
		daoFactory.commitTransaction();
	}
	
	
	public void persistInsert() {
		daoFactory.beginTransaction();
		daoFactory.createPlantDao().insert(getModel());
		daoFactory.commitTransaction();
	}
	
	public void persistUpdate() {
		daoFactory.beginTransaction();
		daoFactory.createPlantDao().update(getModel());
		daoFactory.commitTransaction();
	}
	
	public List<Unit> persistSearchAllUnit() {
		daoFactory.beginTransaction();
		List<Unit> listUnit = daoFactory.createUnitDao().searchAll();
		daoFactory.commitTransaction();
		return listUnit;
	}
	
	public List<Plant> persistSearchByExample(Plant example) {
		daoFactory.beginTransaction();
		List<Plant> plantList = daoFactory.createPlantDao().searchByExample(example);
		daoFactory.commitTransaction();
		return plantList;
	}
	
	public List<Plant> persistSearchByUnit(Unit unit) {
		daoFactory.beginTransaction();
		List<Plant> plantList = daoFactory.createPlantDao().searchByUnit(unit);
		daoFactory.commitTransaction();
		return plantList;
	}

}
